package com.sk.bilibili.other;

import com.sk.bilibili.utils.NameUtils;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author 邵康
 * @date 2021/11/24 18:12
 */
public class ExtraMap extends HashMap<String,Object> {

    private static final Class<List> LIST_CLASS=List.class;

    public void addExtra(Object extra){
        String key = generateKey(extra);
        put(key,extra);
    }

    private String generateKey(Object obj) {
        Class<?> clazz = obj.getClass();
        if(clazz.isArray()){
            Class<?> sonClass = clazz.getComponentType();
            return NameUtils.lowerFirst(sonClass.getSimpleName())+"s";
        }else if(LIST_CLASS.isAssignableFrom(clazz)){
            List list= (List) obj;
            if(list.isEmpty()){
                System.err.println(obj+"没有元素没有被正常映射，请手动映射");
                return "list";
            }else {
                Class<?> sonClass = list.get(0).getClass();
                return NameUtils.lowerFirst(sonClass.getSimpleName())+"s";
            }
        }else {
            return NameUtils.lowerFirst(clazz.getSimpleName());
        }
    }

}
